"""音乐控制模块"""
import os

import pygame


class MusicPlayer(object):
    """音乐播放器"""
    res_path = 'res/sound/'

    def __init__(self, music_file):
        """初始化音乐播放器"""
        # 加载背景音乐
        pygame.mixer.music.load(self.res_path + music_file)
        pygame.mixer.music.set_volume(0.2)

        # 初始化音效字典
        self.sound_dict = {}

        files = os.listdir(self.res_path)
        for file in files:
            if file == music_file:  # 是背景音乐不需要处理
                continue

            sound = pygame.mixer.Sound(self.res_path + file)
            self.sound_dict[file] = sound

    @staticmethod  # 静态方法可以不传参数self
    def play_music():
        """ 播放背景音乐"""
        pygame.mixer.music.play(-1)

    @staticmethod
    def pause_music(is_pause):
        """根据暂停状态决定是否播放"""
        if is_pause:
            pygame.mixer.music.pause()
        else:
            pygame.mixer.music.unpause()

    def play_sound(self,wav_name):
        """根据文件名，播放音效"""
        self.sound_dict[wav_name].play()